 package client.inventory;

 import client.MapleCharacter;
 import com.alibaba.druid.pool.DruidPooledConnection;
 import com.mysql.jdbc.exceptions.MySQLQueryInterruptedException;
 import database.DatabaseConnection;
 import java.io.Serializable;
 import java.sql.PreparedStatement;
 import java.sql.ResultSet;
 import java.sql.SQLException;
 import java.util.Comparator;
 import org.apache.logging.log4j.LogManager;
 import org.apache.logging.log4j.Logger;
 import server.MapleInventoryManipulator;

 public class MapleRing
   implements Serializable
 {
/*  19 */   private static final Logger log = LogManager.getLogger(MapleRing.class);
   private static final long serialVersionUID = 9179541993413738579L;
   private final int ringId;
   private final int partnerRingId;
   private final int partnerId;
   private final int itemId;
   private String partnerName;
   private boolean equipped = false;

   private MapleRing(int ringId, int partnerRingId, int partnerId, int itemid, String partnerName) {
/*  29 */     this.ringId = ringId;
/*  30 */     this.partnerRingId = partnerRingId;
/*  31 */     this.partnerId = partnerId;
/*  32 */     this.itemId = itemid;
/*  33 */     this.partnerName = partnerName;
   }

   public static MapleRing loadFromDb(int ringId) {
/*  37 */     return loadFromDb(ringId, false);
   }

   public static MapleRing loadFromDb(int ringId, boolean equipped) {
/*  41 */     try (DruidPooledConnection con = DatabaseConnection.getInstance().getConnection()) {
/*  42 */       PreparedStatement ps = con.prepareStatement("SELECT * FROM rings WHERE ringId = ?");
/*  43 */       ps.setInt(1, ringId);
/*  44 */       ResultSet rs = ps.executeQuery();
/*  45 */       MapleRing ret = null;
/*  46 */       if (rs.next()) {
/*  47 */         ret = new MapleRing(ringId, rs.getInt("partnerRingId"), rs.getInt("partnerChrId"), rs.getInt("itemid"), rs.getString("partnerName"));
/*  48 */         ret.setEquipped(equipped);
       }
/*  50 */       rs.close();
/*  51 */       ps.close();
/*  52 */       return ret;
/*  53 */     } catch (SQLException ex) {
/*  54 */       log.error("加载戒指信息出错", ex);
/*  55 */       return null;
     }
   }

   public static void addToDB(int itemid, MapleCharacter player, String partnerName, int partnerId, int[] ringId) throws SQLException {
/*  60 */     try (DruidPooledConnection con = DatabaseConnection.getInstance().getConnection()) {
/*  61 */       PreparedStatement ps = con.prepareStatement("INSERT INTO rings (ringId, itemid, partnerChrId, partnerName, partnerRingId) VALUES (?, ?, ?, ?, ?)");
/*  62 */       ps.setInt(1, ringId[0]);
/*  63 */       ps.setInt(2, itemid);
/*  64 */       ps.setInt(3, player.getId());
/*  65 */       ps.setString(4, player.getName());
/*  66 */       ps.setInt(5, ringId[1]);
/*  67 */       ps.executeUpdate();
/*  68 */       ps.close();

/*  70 */       ps = con.prepareStatement("INSERT INTO rings (ringId, itemid, partnerChrId, partnerName, partnerRingId) VALUES (?, ?, ?, ?, ?)");
/*  71 */       ps.setInt(1, ringId[1]);
/*  72 */       ps.setInt(2, itemid);
/*  73 */       ps.setInt(3, partnerId);
/*  74 */       ps.setString(4, partnerName);
/*  75 */       ps.setInt(5, ringId[0]);
/*  76 */       ps.executeUpdate();
/*  77 */       ps.close();
     }
   }

   public static int createRing(int itemId, MapleCharacter player, String partnerName, String msg, int partnerId, int itemSn) {
     try {
/*  83 */       if (player == null)
/*  84 */         return -2; 
/*  85 */       if (partnerId <= 0) {
/*  86 */         return -1;
       }
/*  88 */       return makeRing(itemId, player, partnerName, partnerId, msg, itemSn);
/*  89 */     } catch (Exception ex) {
/*  90 */       log.error("创建戒指信息出错", ex);
/*  91 */       return 0;
     }
   }


   public static int[] makeRing(int itemId, MapleCharacter player, MapleCharacter partnerPlayer) throws Exception {
/*  97 */     int[] makeRingId = { MapleInventoryIdentifier.getInstance(), MapleInventoryIdentifier.getInstance() };
     try {
/*  99 */       addToDB(itemId, player, partnerPlayer.getName(), partnerPlayer.getId(), makeRingId);
/* 100 */     } catch (MySQLQueryInterruptedException mslcve) {
/* 101 */       return makeRingId;
     }
/* 103 */     return makeRingId;
   }


   public static int makeRing(int itemId, MapleCharacter player, String partnerName, int partnerId, String msg, int itemSn) throws Exception {
/* 108 */     int[] makeRingId = { MapleInventoryIdentifier.getInstance(), MapleInventoryIdentifier.getInstance() };
     try {
/* 110 */       addToDB(itemId, player, partnerName, partnerId, makeRingId);
/* 111 */     } catch (MySQLQueryInterruptedException mslcve) {
/* 112 */       return 0;
     }
/* 114 */     MapleInventoryManipulator.addRing(player, itemId, makeRingId[1], itemSn);
/* 115 */     player.getCashInventory().gift(partnerId, player.getName(), msg, itemSn, makeRingId[0]);
/* 116 */     return 1;
   }

   public static void removeRingFromDb(int ringId, int partnerRingId) {
/* 120 */     try (DruidPooledConnection con = DatabaseConnection.getInstance().getConnection()) {
/* 121 */       PreparedStatement ps = con.prepareStatement("DELETE FROM rings WHERE ringId = ? OR ringId = ?");
/* 122 */       ps.setInt(1, ringId);
/* 123 */       ps.setInt(2, partnerRingId);
/* 124 */       ps.executeUpdate();
/* 125 */       ps.close();
/* 126 */     } catch (SQLException ex) {
/* 127 */       log.error("删除戒指信息出错", ex);
     }
   }

   public static void removeRingFromDb(MapleCharacter player) {
/* 132 */     try (DruidPooledConnection con = DatabaseConnection.getInstance().getConnection()) {
/* 133 */       PreparedStatement ps = con.prepareStatement("SELECT * FROM rings WHERE partnerChrId = ?");
/* 134 */       ps.setInt(1, player.getId());
/* 135 */       ResultSet rs = ps.executeQuery();
/* 136 */       if (!rs.next()) {
/* 137 */         ps.close();
/* 138 */         rs.close();
         return;
       }
/* 141 */       int otherId = rs.getInt("partnerRingId");
/* 142 */       int otherotherId = rs.getInt("ringId");
/* 143 */       rs.close();
/* 144 */       ps.close();
/* 145 */       ps = con.prepareStatement("DELETE FROM rings WHERE ringId = ? OR ringId = ?");
/* 146 */       ps.setInt(1, otherotherId);
/* 147 */       ps.setInt(2, otherId);
/* 148 */       ps.executeUpdate();
/* 149 */       ps.close();
/* 150 */     } catch (SQLException ex) {
/* 151 */       log.error("删除戒指信息出错", ex);
     }
   }

   public int getRingId() {
/* 156 */     return this.ringId;
   }

   public int getPartnerRingId() {
/* 160 */     return this.partnerRingId;
   }

   public int getPartnerChrId() {
/* 164 */     return this.partnerId;
   }

   public int getItemId() {
/* 168 */     return this.itemId;
   }

   public boolean isEquipped() {
/* 172 */     return this.equipped;
   }

   public void setEquipped(boolean equipped) {
/* 176 */     this.equipped = equipped;
   }

   public String getPartnerName() {
/* 180 */     return this.partnerName;
   }

   public void setPartnerName(String partnerName) {
/* 184 */     this.partnerName = partnerName;
   }


   public boolean equals(Object o) {
/* 189 */     return (o instanceof MapleRing && ((MapleRing)o).getRingId() == getRingId());
   }


   public int hashCode() {
/* 194 */     int hash = 5;
/* 195 */     hash = 53 * hash + this.ringId;
/* 196 */     return hash;
   }


   public static class RingComparator
     implements Comparator<MapleRing>, Serializable
   {
     private static final long serialVersionUID = -1052568707811904914L;

     public int compare(MapleRing o1, MapleRing o2) {
/* 206 */       if (o1.ringId < o2.ringId)
/* 207 */         return -1; 
/* 208 */       if (o1.ringId == o2.ringId) {
/* 209 */         return 0;
       }
/* 211 */       return 1;
     }
   }
 }


/* Location:              C:\PGX\Server.jar!\client\inventory\MapleRing.class
 * Java compiler version: 8 (52.0)
 * JD-Core Version:       1.1.3
 */